package com.example.aspectj.aspect;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.aspectj.LoginActivity;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoginCheckAspect {
    private static final String TAG = LoginCheckAspect.class.getSimpleName();

    @Pointcut("execution(@com.example.aspectj.annotation.LoginCheck * *(..))")
    public void methodLoginCheck() {
    }

    @Around("methodLoginCheck()")
    public Object check(ProceedingJoinPoint joinPoint) throws Throwable {
        Context context = (Context) joinPoint.getThis();
        boolean isLogin = false; // 实际项目中，从SharedPreference 从读取
        if (isLogin) {
            Log.e(TAG, "检测到已登录");
            return joinPoint.proceed();
        } else {
            Log.e(TAG, "检测到未登录");
            context.startActivity(new Intent(context, LoginActivity.class));
            return null; //未登录情况下，后续代码都不需要执行了。
        }
    }
}
